const mysql = require('mysql');
const request = require('request'); 

var totalItemCount;
var curCount = 0;

// 数据库配置
const connection = mysql.createConnection({
	host:     '39.106.160.61',
	user:     'root',
	password: '123',
	database: 'test'
});


const w_s_sql = 'UPDATE word SET importance = ?, statistic = ? WHERE id = ?';
const query_w_sql = 'SELECT * FROM word WHERE importance IS NULL OR statistic IS NULL';

connection.query(query_w_sql, (err, word_list) => {
	if (err) {
		console.log('[INSERT ERROR] - ', err.message);
		return;
	}
	totalItemCount = word_list.length;
	for (let word of word_list) {
		updateWordCount(word);
	}
});

function getYear(str) {
	return str.split('-')[1];
}

function getYearMap(sentences_infos) {
	const yearMap = new Map();
	for (let info of sentences_infos) {
		const year = getYear(info[0]);
		if (!yearMap.get(year)) {
			yearMap.set(year, 1);
		} else {
			yearMap.set(year, yearMap.get(year) + 1);
		}
	}
	return yearMap;
}

function updateWordCount(word) {
	var options = {
		url: `http://39.105.43.71:4000/api/v1/word_sentence/${word.english}`
	};
	
	request.get(options, function(err, response, body){
		if (err) {
			console.log(`err in word ${word.english}`);
			console.info(err);
			return;
		}
		const sentences_infos = JSON.parse(response.body).sentences_info;
		if (sentences_infos === 'no sentences' || sentences_infos === []) {
			return;
		}
		const yearMap = getYearMap(sentences_infos);
		const importance = sentences_infos.length;
		const statistic = [...yearMap].map(([year, count]) => {
			return `${year},${count}`;
		}).join(';');

		let sqlParams = [importance, statistic, word.id];
		connection.query(w_s_sql, sqlParams, (err) => {
			if (err) {
				console.log(`${word.english}-[INSERT ERROR] - `, err.message);
				return;
			}
		});
	});
}